import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import _ from 'lodash'
import qs from 'qs'
const schemaUrl = CommonConfig.schemaUrl + '/v1'
const baseUrl = CommonConfig.machinestatusUrl + '/v1'
const reportingUrl = CommonConfig.reportUrl + '/v1'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

// 获取车间
export function getWorkshops () {
  const newUrl = `${schemaUrl}/workshop_kv`
  const params = {
    withPlantName: false
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 根据车间ID 获取车间在用班制
 * id 车间ID
 */
export function getShifts (id) {
  if (_.isNil(id)) {
    return
  }
  const newUrl = `${schemaUrl}/workshop/${id}/shifts/status:use/id`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 列举设备开动率
 * start 开始时间
 * end 结束时间
 * id 车间ID
 * shiftId 班制id
 */
export function getDeviceRate (params, id) {
  const newUrl = `${baseUrl}/workshops/${id}/device_operating_rate`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res.data)
  })
}
/**
 * 根据车间id查询产线
 */
export function getLinesByWorkshop (workShopId) {
  const newUrl = `${schemaUrl}/lines_by_workshop/filter_default`
  return axiosInstance.get(newUrl, { params: { workShopId } }).then((res) => {
    return Promise.resolve(res.data)
  })
}
/**
 * 查询当班状态分布
 * start 班制开始时间
 * workShopId 车间id
 * **/
export function getCurStatus (params, workshopId) {
  const newUrl = `${baseUrl}/workshops/${workshopId}/shift_status_chart`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 停机原因未填写页
 * @param params
 */
export function getStopNotWrite (params) {
  const newUrl = `${baseUrl}/list_device/stop_causes/not_write`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res.data)
  })
}

/**
 * 实时作业状态分布
 * **/
export function getStatisticsState (params, workshopId) {
  const newUrl = `${baseUrl}/workshops/${workshopId}/statistics_state`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res.data)
  })
}
/**
 * 获取大屏稼动率报表数据
 * workshopId  车间id
 * startDate 开始日期
 * endDate 结束日期
 * tab 报表类型 ['daily', 'weekly', 'monthly]
 **/
export function getOeeInfo (workshopId, params, tab = 'daily') {
  const newUrl = `${reportingUrl}/dashboard/workshops/${workshopId}/oee_${tab}_reports`
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取设备数量
 * @param params
 * @returns {Promise<unknown>}
 */
export function getCountDevice (params) {
  const reqData = {
    params
  }
  const newUrl = `${baseUrl}/workshop/count_device`

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}

/**
 * 获取实时状态
 * @param params
 * @returns {Promise<unknown>}
 */
export function getRealtimeStatus (params) {
  const reqData = {
    params
  }
  const newUrl = `${baseUrl}/workshops/real_time_status`

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
export function getStopChart (params) {
  const newUrl = `${baseUrl}/workshops/device_status_durations:chart`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function getStopList (params) {
  const newUrl = `${baseUrl}/workshops/device_status_durations:list`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res.data)
  })
}
export function getCauseItem (params) {
  const reqData = {
    params
  }
  const newUrl = `${baseUrl}/device/stop_cause_item`

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function getAlarmChart (params) {
  const reqData = {
    params: {
      ...params,
      limit: 5
    }
  }
  const newUrl = `${baseUrl}/devices/alarm_time:chart`

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function getAlarmList (params) {
  const reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const newUrl = `${baseUrl}/devices/alarm_count_time:list`

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
/**
 * 获取报警类别
 */
export function getAlarmTypeList (params) {
  const reqData = {
    params
  }
  const url = baseUrl + '/devices/alarm:type'
  return axiosInstance.get(url, reqData).then((res) => {
    return Promise.resolve((res.data))
  })
}
/**
 * 获取报警类别
 */
export function getStopTypeList (params) {
  const reqData = {
    params
  }
  const url = baseUrl + '/device/stop_cause_list'
  return axiosInstance.get(url, reqData).then((res) => {
    return Promise.resolve((res.data))
  })
}
